package com.yikaiye.android.yikaiye.ui.activity.login;

import android.os.Bundle;
import android.text.TextUtils;

import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.base.BaseActivity;
import com.yikaiye.android.yikaiye.databinding.ActivityFindPasswordBinding;
import com.yikaiye.android.yikaiye.ui.activity.login.viewmodel.FindPasswordViewModel;
import com.yikaiye.android.yikaiye.utils.JumpIntent;
import com.yikaiye.android.yikaiye.utils.ToastHelper;

/**
 * 找回密码
 */
public class FindPasswordActivity extends BaseActivity<ActivityFindPasswordBinding, FindPasswordViewModel> {

    @Override
    protected FindPasswordViewModel bindModel() {
        return getViewModel(this, FindPasswordViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initData() {
        binding.setFindPasswordViewModel(viewModel);
    }

    @Override
    protected void initClick() {
        viewModel.onDelayClick(binding.back, o -> finish());
        viewModel.onDelayClick(binding.cdtCountdown, o -> {
            if (!TextUtils.isEmpty(viewModel.phoneNumber.getValue())) {
                showLoadingDialog();
                viewModel.sendCode(5,viewModel.phoneNumber.getValue());
            } else {
                ToastHelper.show(getString(R.string.please_input_cell_number));
            }
        });
        viewModel.onDelayClick(binding.btnNext, o -> {
            Bundle bundle = new Bundle();
            bundle.putString("phone", viewModel.phoneNumber.getValue());
            bundle.putString("code", viewModel.code.getValue());
            JumpIntent.jump(this, SetPasswordActivity.class, bundle);
        });

        viewModel.phoneNumber.observe(this, s -> changeBtnStatus());
        viewModel.code.observe(this, s -> changeBtnStatus());

        // 获取验证码成功
        viewModel.codeSuccess.observe(this, verifyCodeModel -> {
            dismissLoadingDialog();
            binding.cdtCountdown.startCountDown(
                    context,
                    60,
                    getString(R.string.send_code_again),
                    "s"
            );
            ToastHelper.show(getString(R.string.code_has_been_sent));
        });
        // 获取验证码失败
        viewModel.codeError.observe(this, s -> {
            dismissLoadingDialog();
            ToastHelper.show(s);
        });
    }

    private void changeBtnStatus() {
        if (!TextUtils.isEmpty(viewModel.phoneNumber.getValue())
                && !TextUtils.isEmpty(viewModel.code.getValue())) {
            binding.btnNext.setEnabled(true);
            binding.btnNext.setBackgroundResource(R.drawable.shape_f93f50_4dp);
        } else {
            binding.btnNext.setEnabled(false);
            binding.btnNext.setBackgroundResource(R.drawable.shape_fcebea_4dp);
        }
    }
}